//
// definition of title scene
//
var TitleScene = {
    scene:null,
    background:null,
    logo:null,
    buttonStart:null,
    buttonCredit:null,
    buttonHowto:null,
    init:function() {
        this.scene = quicktigame2d.createScene();

        var onloadsprite = (function(self) {
            return function(e) {
                Ti.API.info("onloadsprite: " + e.name);
                if (e.name == "graphics/title/button_howto.png") {
                    game.startCurrentScene();
	                if (!titleBGM.isPlaying()) {
    	                titleBGM.play();
        	            titleBGM.setLooping(true);
            	    }
                }
            };
        })(this);

        var touchstart = (function(self) {
            return function(e) {
                // fix coordinates because view touch event returns non-retina values.
                var x = e.x * WINDOW_SCALE_FACTOR_X;
                var y = e.y * WINDOW_SCALE_FACTOR_Y;
                
                if (self.buttonStart.contains(x, y)) {
                    self.buttonStart.frame = 0;
                    picoSE.play();
                }
                if (self.buttonCredit.contains(x, y)) {
                    self.buttonCredit.frame = 0;
                    picoSE.play();
                }
                if (self.buttonHowto.contains(x, y)) {
                    self.buttonHowto.frame = 0;
                    picoSE.play();
                }
            };
        })(this);
        
        var touchend = (function(self) {
            return function(e) {
                // fix coordinates because view touch event returns non-retina values.
                var x = e.x * WINDOW_SCALE_FACTOR_X;
                var y = e.y * WINDOW_SCALE_FACTOR_Y;
                
                self.buttonStart.frame = 1;
                self.buttonCredit.frame = 1;
                self.buttonHowto.frame = 1;
                
                if (self.buttonStart.contains(x, y)) {
                    titleBGM.stop();
                    titleBGM = null;
                    game.pushScene(MainLoadingScene.scene);
                }
                if (self.buttonCredit.contains(x, y)) {
                    game.pushScene(CreditScene.scene);
                }
                if (self.buttonHowto.contains(x, y)) {
                    game.pushScene(HowtoScene.scene);
                }
              };
        })(this);
        
        var activated = (function(self) {
            return function(e) {
                Ti.API.info("title scene is activated");
    
                // Set background color to black
                self.scene.color(0, 0, 0);
                
                if (titleBGM == null) {
                    titleBGM = Ti.Media.createSound({url:'/music/title.wav'});
                }
                if (picoSE == null) {
                    picoSE  = Ti.Media.createSound({url:'/se/pico.wav'});
                }
                
                // Create sprite instance    
                if (self.background == null) {
                    self.background = quicktigame2d.createSprite({image:'graphics/title/title_background.png'});
                }
                if (self.logo == null) {
                    self.logo = quicktigame2d.createSprite({image:'graphics/title/logo.png'})
                }
                if (self.buttonStart == null) {
                    self.buttonStart = quicktigame2d.createSpriteSheet({image:'graphics/title/button_start.png', width:138, height:50});
                }
                if (self.buttonCredit == null) {
                    self.buttonCredit = quicktigame2d.createSpriteSheet({image:'graphics/title/button_credit.png', width:138, height:50});
                }
                if (self.buttonHowto == null) {
                    self.buttonHowto = quicktigame2d.createSpriteSheet({image:'graphics/title/button_howto.png', width:138, height:50});
                }
    
    			// set background at center of the screen
                self.background.x = (game.screen.width   * 0.5) - (self.background.width   * 0.5);
                self.background.y = (game.screen.height  * 0.5) - (self.background.height  * 0.5);
                
                self.logo.x = (game.screen.width  * 0.5) - (self.logo.width  * 0.5);
    
                var buttonMargin = 10;
                self.buttonStart.x = (game.screen.width * 0.5) - (self.buttonStart.width * 0.5);
                self.buttonStart.y = game.screen.height - self.buttonStart.height - buttonMargin;
    
                self.buttonCredit.x = self.buttonStart.x + self.buttonCredit.width + buttonMargin;
                self.buttonCredit.y = self.buttonStart.y;
                self.buttonHowto.x  = self.buttonStart.x - self.buttonHowto.width - buttonMargin;
                self.buttonHowto.y  = self.buttonStart.y;
    
                self.buttonStart.frame  = 1;
                self.buttonCredit.frame = 1;
                self.buttonHowto.frame  = 1;

                self.scene.add(self.background);
                self.scene.add(self.logo);
                self.scene.add(self.buttonStart);
                self.scene.add(self.buttonCredit);
                self.scene.add(self.buttonHowto);
                
                game.addEventListener('touchstart', touchstart);
                game.addEventListener('touchend', touchend);
              };
        })(this);
        
        var deactivated = (function(self) {
            return function(e) {
                Ti.API.info("title scene is deactivated");
            
                // remove logo from the scene and set variable null
                // that triggers removal from the memory at GC.
                self.scene.remove(self.background);
                self.background = null;
    
                self.scene.remove(self.logo);
                self.logo = null;

                self.scene.remove(self.buttonStart);
                self.scene.remove(self.buttonCredit);
                self.scene.remove(self.buttonHowto);
                self.buttonStart = null;
                self.buttonCredit = null;
                self.buttonHowto = null;
    
                // If you would like to remove cached texture resource from the memory,
                // use GameView.unloadTexture(texture_name) that unloads texture resource with given name.
                game.unloadTexture('graphics/title/button_start.png');
                game.unloadTexture('graphics/title/button_credit.png');
                game.unloadTexture('graphics/title/button_howto.png');
    
                // Stop handling touch event
                game.removeEventListener('touchstart', touchstart);
                game.removeEventListener('touchend', touchend);
              };
        })(this);
        
        // register scene activationi event
        this.scene.addEventListener('activated',   activated);
        this.scene.addEventListener('deactivated', deactivated);
        this.scene.addEventListener('onloadsprite',  onloadsprite);
    },
};
